package service.impl;

import entity.SocialServices;
import mapper.SocialServicesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.SocialServicesService;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SocialServicesImpl implements SocialServicesService {
    @Autowired private SocialServicesMapper mapper;
    public SocialServices getById(Integer id) { return mapper.selectById(id); }
    public List<SocialServices> getAll() { return mapper.selectAll(); }
    public List<SocialServices> getByStudentId(Integer sid) { return mapper.selectByStudentId(sid); }
    public int create(SocialServices s) {
        // 数据验证
        if (s.getServiceType() == null || s.getServiceType().trim().isEmpty()) {
            throw new IllegalArgumentException("服务类型不能为空");
        }
        if (s.getStudentId() == null) {
            throw new IllegalArgumentException("学生ID不能为空");
        }
        if (s.getScore() == null) {
            s.setScore(0.0f);
        }
        if (s.getApproved() == null) {
            s.setApproved(true);
        }

        s.setCreatedAt(LocalDateTime.now());
        return mapper.insert(s);
    }
    public int update(SocialServices s) {
        // 数据验证
        if (s.getServiceType() == null || s.getServiceType().trim().isEmpty()) {
            throw new IllegalArgumentException("服务类型不能为空");
        }
        if (s.getStudentId() == null) {
            throw new IllegalArgumentException("学生ID不能为空");
        }
        if (s.getScore() == null) {
            s.setScore(0.0f);
        }
        if (s.getApproved() == null) {
            s.setApproved(true);
        }

        s.setUpdatedAt(LocalDateTime.now());
        return mapper.update(s);
    }
    public int delete(Integer id) { return mapper.delete(id); }
}